package com.itsm.controller;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itsm.config.WeComProperties;
import com.itsm.entity.FileName;
import com.itsm.entity.User;
import com.itsm.service.AccessTokenService;
import com.itsm.service.LoginService;
import com.itsm.service.UrlRequestService;
import com.itsm.service.UserService;
import com.itsm.service.mapper.FileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Controller
public class LoginController {

    @Resource
    private WeComProperties properties;

    @Autowired
    private UserService userService;
    @Autowired
    private UrlRequestService urlRequestService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private AccessTokenService accessTokenService;

    @Autowired
    private FileMapper fileMapper;

    @RequestMapping(value="/login" , method = RequestMethod.GET)
    public String login(){
        return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwfbc200b28be3408d&redirect_uri=http://itservice.hmjy-ic.com:8098/getWeUser1&response_type=code&scope=snsapi_privateinfo&state=STATE&agentid=1000023#wechat_redirect";
    }

    @RequestMapping(value="/loginCreate" , method = RequestMethod.GET)
    public String create(){
        return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwfbc200b28be3408d&redirect_uri=http://itservice.hmjy-ic.com:8098/create&response_type=code&scope=snsapi_privateinfo&state=STATE&agentid=1000023#wechat_redirect";
    }



    @RequestMapping(value = "/create")
    public Object create(String code,HttpServletRequest request) throws Exception {


        JSONObject jsonObject1 = this.getInfo(code);
        String depart = null;
        String sup = null;
        String name = jsonObject1.getString("name");
        String userid = jsonObject1.getString("userid");
        String alias = jsonObject1.getString("alias");
//        List<String> supList = (List<String>) jsonObject1.get("direct_leader");
        List<String> supList = JSONObject.parseArray(JSONObject.toJSONString(jsonObject1.get("direct_leader")), String.class);
        if(supList.size()>0){
            sup = supList.get(0);
        }
//        List<String> departList = (List<String>) jsonObject1.get("department");
        List<String> departList = JSONObject.parseArray(JSONObject.toJSONString(jsonObject1.get("department")), String.class);
        if(departList.size()>0){
            depart = departList.get(0);
        }
        request.getSession().setAttribute("name",name);
        request.getSession().setAttribute("userid",userid);
        request.getSession().setAttribute("alias",alias);
        User user1 = userService.selectUser(userid);
        if(user1==null){
            User user = new User();
            Integer plant = fileMapper.getPlant(alias);
            user.setName(name);
            user.setWxid(userid);
            user.setPlant(plant);
            user.setSupWxid(sup);
            user.setDepart(depart);
            userService.addUser(user);
        }
//        else {
//            request.getSession().setAttribute("post",user1.getPost());
//            request.getSession().setAttribute("approve",user1.getApprove());
//        }

        return "redirect:/create.html";
    }

    @RequestMapping(value = "/getWeUser1")
    public Object getWeUser(String code,HttpServletRequest request) throws Exception {


        JSONObject jsonObject1 = this.getInfo(code);

        String sup = null;
        String depart = null;
        String name = jsonObject1.getString("name");
        String userid = jsonObject1.getString("userid");
        String alias = jsonObject1.getString("alias");
//        List<String> supList = (List<String>) jsonObject1.get("direct_leader");
        List<String> supList = JSONObject.parseArray(JSONObject.toJSONString(jsonObject1.get("direct_leader")), String.class);
        if(supList.size()>0){
            sup = supList.get(0);
        }

//        List<String> departList = (List<String>) jsonObject1.get("department");
        List<String> departList = JSONObject.parseArray(JSONObject.toJSONString(jsonObject1.get("department")), String.class);
        if(departList.size()>0){
            depart = departList.get(0);
        }
        request.getSession().setAttribute("name",name);
        request.getSession().setAttribute("userid",userid);
        request.getSession().setAttribute("alias",alias);
        User user1 = userService.selectUser(userid);
        if(user1==null){
            User user = new User();
            Integer plant = fileMapper.getPlant(alias);
            user.setName(name);
            user.setWxid(userid);
            user.setPlant(plant);
            user.setDepart(depart);
            user.setSupWxid(sup);
            userService.addUser(user);
        }

//        else {
//            request.getSession().setAttribute("post",user1.getPost());
//            request.getSession().setAttribute("approve",user1.getApprove());
//        }
        return "redirect:/index.html";
    }




    public JSONObject getInfo(String code) throws Exception {
        HashMap<String,String> param = new HashMap<>();
        String token = accessTokenService.getAccessToken(properties.getCorpId(),properties.getCorpSecret());
        param.put("access_token", token);
        param.put("code", code);
        Object re = urlRequestService.requestGetUrl("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo",param,null);
        JSONObject jsonObject = JSONObject.parseObject(re.toString());
        HashMap<String,String> param1 = new HashMap<>();
        param1.put("access_token", token);
        param1.put("userid", jsonObject.getString("userid"));
        JSONObject re1 = (JSONObject) urlRequestService.requestGetUrl("https://qyapi.weixin.qq.com/cgi-bin/user/get",param1,null);
        return JSONObject.parseObject(re1.toString());
    }





}
